<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Shop\Controller;

use Common\Util\Check;
use Shop\Api\AddressApi;
use Shop\Api\MemberConfigApi;
use Tool\Api\AreaApi;
use Tool\Api\CityApi;
use Tool\Api\ProvinceApi;

class AddressController extends ShopController{
	
	/**
	 * 收货地址选择
	 */
	public function choose(){
		$map = array();
		$map['wxuserid'] = $this->userinfo['id'];
		
		$result = apiCall(AddressApi::QUERY, array($map));
		
		if(!$result['status']){
			$this->error($result['info']);
		}

		
		$this->assign("list",$result['info']['list']);
		
		$this ->theme($this->themeType)->display();
		
	}

	public function add(){
		if(IS_GET){
		
			
            cookie("__forward__",$_SERVER['HTTP_REFERER']);
			$map = array();
			$map['uid'] = $this->userinfo['id'];
			$map['default'] = 1;

			$result = apiCall(AddressApi::GET_INFO,array($map));

			if(!$result['status']){
				$this->error($result['info']);
			}
			if(is_array($result['info'])){
				//获取城市，区域信息
				$city  = apiCall( CityApi::GET_LIST_BY_PROVINCE_ID, array($result['info']['province']));
				if(!$city['status']){
					LogRecord($city['info'], __FILE__.__LINE__);
				}
				$this->assign("city",$city['info']);
				$area  = apiCall(AreaApi::GET_LIST_BY_CITY_ID , array($result['info']['city']));
				if(!$area['status']){
					LogRecord($city['info'], __FILE__.__LINE__);
				}
				$this->assign("area",$area['info']);
			}

			$this->assign("address",$result['info']);
			
			$result = apiCall(ProvinceApi::QUERY_NO_PAGING, array());
			
			if(!$result['status']){
				$this->error($result['info']);
			}
			$this->assign("provinces",$result['info']);
			$this->assign("back_url",cookie("__forward__"));
			$this ->theme($this->themeType)->display();
		}else{

			$id = I('post.id',0,'intval');
            $province = I('post.province','');
            $city = I('post.city','');
            $area = I('post.area','');
            $provinceid = $this->_post('provinceid','','省份必须填写');
            $cityid = $this->_post('cityid','','城市必须填写');
            $areaid = $this->_post('areaid','','区域必须填写');

			$detailinfo = $this->_post('detailinfo','','详细地址必须填写');
			$mobile = I('post.mobile','');
			$postal_code = I('post.postal_code','');
            $contactname = $this->_post('contactname','','收货人必须填写');
            $default = I('post.default',0);
            $uid = $this->userinfo['id'];
            $id_card = $this->_post('id_card','','身份证号必须');

            $check = new Check();
            if(!$check->is_ID_Card($id_card)){
                $this->error("请填写真实的身份证!");
            }


			$entity = array(
				'wxno'=>'',
				'country'=>'中国',
                'province'=>$province,
                'city'=>$city,
                'area'=>$area,
                'provinceid'=>$provinceid,
                'cityid'=>$cityid,
                'areaid'=>$areaid,
				'detailinfo'=>$detailinfo,
				'mobile'=>$mobile,
				'contactname'=>$contactname,			
				'postal_code'=>$postal_code,
                'id_card'=>$id_card
			);

			if(empty($id)){
				//新增
				$entity['uid'] = $uid;
				$result = apiCall(AddressApi::ADD,array($entity));
                if($result['status']){
                    $id = $result['info'];
                }
			}else{
				//保存
				$result = apiCall(AddressApi::SAVE_BY_ID,array($id,$entity));
			}

            if($default == 1){
                $result = apiCall(MemberConfigApi::SAVE,array(array('uid'=>$uid),array('default_address'=>$id)));
                if(!$result['status']){
                    LogRecord($result['info'],__FILE__.__LINE__);
                }else{
                    $this->updateDefaultAddress($id);
                }
            }

			if(!$result['status']){
				$this->error($result['info']);
			}

			$this->success("操作成功!~");
			
		}
	}


    /*
     * 删除地址
     * */
    public function delete(){
        $id= $this->_get('id',0,"缺失ID参数!");

        $map=array('id'=>$id);
        $result=apiCall(AddressApi::DELETE,array($map));
        if($result['status']){
            $this->success('删除地址成功!',U('Shop/User/address'));
        }else{
            $this->error($result['info']);
        }
    }

	
}
